/***********************************************************************
    Written By Alex "SparkyMcSparks" Romo
    
    Questions? Concerns? Suggestions?
        Contact Me On AIM @ DareDevilSparks7
***********************************************************************/

Script.Load( "lua/Utility.lua" )

class 'Galaga_Target' ( Actor )

Galaga_Target.modelName  = "models/alien/skulk/skulk.model"
Galaga_Target.spawnSound = "sound/ns2.fev/alien/skulk/spawn"
Galaga_Target.dieSound   = "sound/ns2.fev/alien/skulk/death"

Galaga_Target.extents   = Vector(0.4064, 0.7874, 0.4064)
Galaga_Target.moveSpeed  =  7

Shared.PrecacheModel( Galaga_Target.modelName )

Shared.PrecacheSound( Galaga_Target.spawnSound )
Shared.PrecacheSound( Galaga_Target.dieSound )

Galaga_Target.State = enum { 'MoveIn', 'Killed' }

Galaga_Target.thinkInterval = 0.18 // 0.25
Galaga_Target.networkVars = 
    {

    }

function Galaga_Target:OnInit()

    Actor.OnInit( self )
    
    self.type = 0
    
    self.velocity  = Vector( 0, 0, 0 )
    
    self:SetModel( Galaga_Target.modelName )
    
    self.moveGroupMask              = 0xFFFFFFFD
    
    if ( Client ) then
        // Blank -- Nothing On The Client Side So Far
    end
    
    if ( Server ) then
        //self.state  = Galaga_Target.State.MoveIn
        
        self:SetNextThink( Galaga_Target.thinkInterval )
    end
    
end

function Galaga_Target:OnLoad()
    Actor.OnLoad( self )
end

function Galaga_Target:GetCanMove()
    return Game.instance:GetHasGameStarted()
end

function Galaga_Target:MoveIn()
    local targetSpawns = getEnt( "target" )
    local angles = Angles( targetSpawns[ 2 ]:GetAngles() )

    local fowardAxis = nil
    local sideAxis   = nil
    
    forwardAxis = Vector( 0, 0, angles.yaw * -1)
    sideAxis    = Vector( angles.yaw * -1, 0, angles.roll  * -1)
    
    forwardAxis:Normalize()
    sideAxis:Normalize()
    
    local canMove = self:GetCanMove()
    
    if( canMove ) then
    
        self.state = Galaga_Target.State.MoveIn
    
        self:SetAnimation( "flinch" ) // Sparks: TODO -- Needs Better Animation, Run Is Same As Idle
        
        // Sparks: TEMP -- Move Straight For Now, Testing
        local wishDirection = forwardAxis * 1
        local wishSpeed = math.min( wishDirection:Normalize(), 1 ) * Galaga_Target.moveSpeed
         
        // Accelerate in the desired direction, ala Quake/Half-Life
        
        local currentSpeed = Math.DotProduct(self.velocity, wishDirection)
        local addSpeed     = wishSpeed - currentSpeed
     
        if (addSpeed > 0) then
            local accelSpeed = math.min(addSpeed, Player.moveAcceleration * 1 * wishSpeed)
            self.velocity = self.velocity + wishDirection * accelSpeed
        end   
        
        local offset = nil
        
        // Sparks: TODO -- Dancing In a la Galaga-style
        self:PerformMovement( self.velocity * 0.04, 5 )
    end
end

function Galaga_Target:PerformMovement( offset, maxTraces )

    local capsuleRadius = self.extents.x
    local capsuleHeight = ( self.extents.y - capsuleRadius ) * 2
    
    local center = Vector( 0, capsuleHeight * 0.5 + capsuleRadius, 0 )
    local origin = Vector( self:GetOrigin() )

    local tracesPerformed = 0

    while ( offset:GetLengthSquared() > 0.0 and tracesPerformed < maxTraces ) do

        local traceStart = origin + center
        local traceEnd = traceStart + offset
        
        local trace = Shared.TraceCapsule( traceStart, traceEnd, capsuleRadius, capsuleHeight, self.moveGroupMask )

        if ( trace.fraction < 1 ) then
        
            offset = offset * ( 1 - trace.fraction )

            offset = offset - offset:GetProjection( trace.normal )
            
            completedSweep = false
            capsuleSweepHit = true
    
        else
            offset = Vector(0, 0, 0)
        end
    
        origin = trace.endPoint - center
        tracesPerformed = tracesPerformed + 1

    end

    self:SetOrigin( origin )
    return origin

end

if ( Server ) then
    function Galaga_Target:TakeDamage( attacker, damage, doer, point, direction ) 
        self:PlaySound( self.dieSound )
        
        self:SetAnimation( "death" )
        
        Game.instance:DestroyTarget( attacker, self )
        
        self.state = Galaga_Target.State.Killed
        
        Server.DestroyEntity( self )
    end

    function Galaga_Target:OnThink()
        Actor.OnThink( self )
        
        // Logic Here For When We Need More Enemies
        local time = Shared.GetTime()
        
        if ( self.state == Galaga_Target.State.Killed ) then
             // Nothing Yet
        else
            self:MoveIn()
        end
        
        self:SetNextThink( Galaga_Target.thinkInterval )
    end 
end

if ( Client ) then
    function Galaga_Target:TakeDamage( attacker, damage, doer, point, direction )
        if (self.model ~= nil) then
            Client.DestroyRenderModel(self.model)
            self.model = nil
        end
    end

    function Galaga_Target:OnSynchronized()
        Actor.OnSynchronized( self )
    end
end

Shared.LinkClassToMap( "Galaga_Target", "galaga_target", Galaga_Target.networkVars )